﻿#region references
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;

/* Project dependencies */
using IcaGames.Database;
using IcaGames.Logging;
using IcaGames.Objects;
#endregion

namespace IcaGames.Database.ObjectManagement
{
    /*
     * Class that holds common methods used to manage Review objects.
     * 
     * @author
     * Rick Oonk
     * 
     * @version
     * 1.0
     * 
     * @update
     * 08-12-2009 by Rick Oonk.
     * Created Class and added attributes and standard functions. 
     */
    public static class ReviewManagement
    {
        /*
         * Function to add a new Game Review into the database.
         * 
         * @param
         * rv: The Review that has to be added.
         * 
         * @returns
         * True if the Review has succesfully been added.
         * False if the Review has not succesfully been added.
         */ 
        public static bool AddReview(Review rv)
        {
            SqlCommand command = null;
            string commandText = "spAddReview";

            try
            {
                command = Connection.GetCommand(commandText);
                command.Parameters.AddWithValue("@gameNr", rv.GameNr);
                command.Parameters.AddWithValue("@userId", rv.UserId);
                command.Parameters.AddWithValue("@text", rv.Text);
                command.Parameters.AddWithValue("@cipher", rv.Cipher);
                command.Parameters.AddWithValue("@date", rv.Date);
                command.CommandType = CommandType.StoredProcedure;
                
                command.ExecuteReader();
            }
            catch (Exception ex)
            {
                Log.CreateLog("Exception while inserting new Review", "REVIEWMANAGEMENT");
                Log.CreateLog(ex.Message, "REVIEWMANAGEMENT");
                return false;
            }
            finally
            {
                Connection.CloseCommand(command);
            }
            return true;
        }

        /*
         * Function to get reviews for the specified Game.
         * 
         * @param
         * gameNr: The gameNr of the specified Game.
         * reviews: The list that has to be loaded with Game reviews.
         * 
         * @returns
         * True if the list has succesfully been loaded.
         * False if the list has not succesfully been loaded.
         */
        public static bool GetReviews(int gameNr, List<Review> reviews)
        {
            SqlCommand command = null;
            SqlDataReader dr = null;
            string commandText = "spGetGameReviews";

            try
            {
                command = Connection.GetCommand(commandText);
                command.Parameters.AddWithValue("@gameNr", gameNr);
                command.CommandType = CommandType.StoredProcedure;

                dr = Connection.GetSqlDataReader(command);
                while (dr.Read())
                {
                    Review rv = new Review
                    {
                        GameNr = Cast.CastToInteger(dr["GAME_NR"]),
                        UserId = Cast.CastToInteger(dr["KLANT_ID"]),
                        Text = Cast.CastToString(dr["TEKST"]),
                        Cipher = Cast.CastToInteger(dr["CIJFER"]),
                        Date = Cast.CastToDateTime(dr["DATUM"])
                    };
                    reviews.Add(rv);
                }
            }
            catch (Exception ex)
            {
                Log.CreateLog("Exception while getting Game reviews", "GAMEMAMANGEMENT");
                Log.CreateLog(ex.Message, "GAMEMAMANGEMENT");
                return false;
            }
            finally
            {
                Connection.CloseCommand(command, dr);
            }

            return true;
        }
    }
}
